package cn.app.edu.projectpracticeofpracticaltraining.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.app.edu.projectpracticeofpracticaltraining.MainActivity;
import cn.app.edu.projectpracticeofpracticaltraining.R;
import cn.app.edu.projectpracticeofpracticaltraining.WebViewActivity;
import cn.app.edu.projectpracticeofpracticaltraining.adapter.BannerHomeFragmentAdapter;
import cn.app.edu.projectpracticeofpracticaltraining.adapter.HomeFragmentAdapter;
import cn.app.edu.projectpracticeofpracticaltraining.constant.ApiInterface;
import cn.app.edu.projectpracticeofpracticaltraining.entity.ArticleEntity;
import cn.app.edu.projectpracticeofpracticaltraining.entity.BannerEntity;
import cn.app.edu.projectpracticeofpracticaltraining.util.HandlerUtil;
import cn.app.edu.projectpracticeofpracticaltraining.util.OkHttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;

import static android.content.ContentValues.TAG;

public class HomeFragment extends Fragment {


    private RecyclerView mRecycler;
    private List<ArticleEntity.DataBean.DatasBean> mBeanList = new ArrayList<>();
    private HomeFragmentAdapter homeFragmentAdapter;
    private Context context;
    private int mCurrentPage = 0;
    private SwipeRefreshLayout mFresh;
    private TextView tvNumber;
    private TextView tvTitle;
    private ViewPager vpImages;
    private View bannerView;


    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_home, null);

        //findviews
        mRecycler = inflate.findViewById(R.id.rv_home_fragment_home);
        mFresh = inflate.findViewById(R.id.srl_fresh_fragment_home);

        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        context = HomeFragment.this.getContext();


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRecycler.setLayoutManager(linearLayoutManager);


        bannerView = LayoutInflater.from(context).inflate(R.layout.banner_home_fragment, mRecycler, false);
        tvNumber = bannerView.findViewById(R.id.tv_number_banner_home_fragment);
        tvTitle = bannerView.findViewById(R.id.tv_title_banner_home_fragment);
        vpImages = bannerView.findViewById(R.id.vp_images_banner_home_fragment);


        homeFragmentAdapter = new HomeFragmentAdapter(context, mBeanList, bannerView, new HomeFragmentAdapter.OnClickListener() {
            @Override
            public void setOnClickListener(View view, int position, ArticleEntity.DataBean.DatasBean datasBean) {

                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("URL", datasBean.getLink());
                intent.putExtra("TITLE", datasBean.getTitle());
                startActivity(intent);

            }

        });
        mRecycler.setAdapter(homeFragmentAdapter);


        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                LinearLayoutManager layoutManager = (LinearLayoutManager) mRecycler.getLayoutManager();
                int itemCount = layoutManager.getItemCount();
                if (itemCount == 0) {
                    return;
                }
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition == itemCount - 1 && newState == RecyclerView.SCROLL_STATE_IDLE) {
                    mCurrentPage += 1;

                    mFresh.setRefreshing(true);

                    getArticle(mCurrentPage);
                }


            }
        });

        mFresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                getArticle(0);

            }
        });


        getArticle(mCurrentPage);
        getBanner();

    }

    /****************************************网络请求**************************************************/
    private void getBanner() {
        OkHttpUtil.enqueue(ApiInterface.BANNER_URL, null, null, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                HandlerUtil.post(new Runnable() {
                    @Override
                    public void run() {
                        new AlertDialog.Builder(context)
                                .setTitle("警告")
                                .setMessage("你的网络似乎有问题，请检查！")
                                .setPositiveButton("确定", null)
                                .show();
                    }
                });
            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                ResponseBody body = response.body();
                String str = body.string();
                final BannerEntity bannerEntity = new Gson().fromJson(str, BannerEntity.class);


                HandlerUtil.post(new Runnable() {
                    @Override
                    public void run() {
                        if (bannerEntity.getErrorCode() == 0) {

                            final List<BannerEntity.DataBean> data = bannerEntity.getData();
                            BannerHomeFragmentAdapter bannerHomeFragmentAdapter = new BannerHomeFragmentAdapter(getContext(), data);
                            vpImages.setAdapter(bannerHomeFragmentAdapter);

                            tvTitle.setText(data.get(0).getTitle());
                            tvNumber.setText("1/" + data.size());

                            vpImages.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                @Override
                                public void onPageScrolled(int i, float v, int i1) {

                                }

                                @SuppressLint("SetTextI18n")
                                @Override
                                public void onPageSelected(int i) {
                                    tvTitle.setText(data.get(i).getTitle());
                                    tvNumber.setText(i + 1 + "/" + data.size());
                                }

                                @Override
                                public void onPageScrollStateChanged(int i) {

                                }
                            });


                        }
                    }
                });

            }
        });
    }


    private void getArticle(int CurrentPage) {
        @SuppressLint("DefaultLocale") String url = String.format(ApiInterface.ARTICLE_URL, CurrentPage);
        OkHttpUtil.enqueue(url, null, null, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                HandlerUtil.post(new Runnable() {
                    @Override
                    public void run() {
                        new AlertDialog.Builder(context)
                                .setTitle("警告")
                                .setMessage("你的网络似乎有问题，请检查！")
                                .setPositiveButton("确定", null)
                                .show();
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                final String result = response.body().string();

//                String result="{\"data\":{\"curPage\":1,\"datas\":[{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15396,\"link\":\"https://www.jianshu.com/p/96830179c418\",\"niceDate\":\"1小时前\",\"niceShareDate\":\"1天前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601191034000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1601026144000,\"shareUser\":\"Android小书生\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"WanAndroid，你来了！\",\"type\":0,\"userId\":76978,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15425,\"link\":\"https://juejin.im/post/6876105622274703368\",\"niceDate\":\"3小时前\",\"niceShareDate\":\"3小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601184056000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1601184056000,\"shareUser\":\"马可没有菠萝\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"面试官：哈希表都不知道，你是怎么看懂HashMap的？\",\"type\":0,\"userId\":27880,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15421,\"link\":\"https://juejin.im/post/6876968255597051917\",\"niceDate\":\"6小时前\",\"niceShareDate\":\"6小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601170825000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1601170825000,\"shareUser\":\"RicardoMJiang\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"三年经验Android开发面经总结\",\"type\":0,\"userId\":13971,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15420,\"link\":\"https://juejin.im/post/6844903843931619336\",\"niceDate\":\"7小时前\",\"niceShareDate\":\"7小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601169049000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1601169049000,\"shareUser\":\"躬行之\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"Material Design组件之NavigationView\",\"type\":0,\"userId\":23270,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":200,\"chapterName\":\"https\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15409,\"link\":\"https://www.jianshu.com/p/c7cde0077892\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134131000,\"realSuperChapterId\":34,\"selfVisible\":0,\"shareDate\":1601128719000,\"shareUser\":\"鸿洋\",\"superChapterId\":98,\"superChapterName\":\"网络访问\",\"tags\":[],\"title\":\"HTTP 的第一次变革 &mdash; HTTP/2\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":458,\"chapterName\":\"mmap\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15410,\"link\":\"https://juejin.im/post/6871265162746314765\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134120000,\"realSuperChapterId\":457,\"selfVisible\":0,\"shareDate\":1601128924000,\"shareUser\":\"鸿洋\",\"superChapterId\":458,\"superChapterName\":\"linux\",\"tags\":[],\"title\":\"Android 筑基 - Linux 系统进程间通信\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":200,\"chapterName\":\"https\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15411,\"link\":\"https://www.jianshu.com/p/5ff622352cbe\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134093000,\"realSuperChapterId\":34,\"selfVisible\":0,\"shareDate\":1601129081000,\"shareUser\":\"鸿洋\",\"superChapterId\":98,\"superChapterName\":\"网络访问\",\"tags\":[],\"title\":\"公钥信任问题 &mdash; 数字证书与 CA\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":432,\"chapterName\":\"UI渲染\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15412,\"link\":\"https://mp.weixin.qq.com/s?__biz=MzI3OTcyNjQ5MQ==&amp;mid=2247483839&amp;idx=1&amp;sn=de79bab8aff5210b076527190dd72d6a&amp;chksm=eb42114bdc35985d64e88c7acad0e4f690c23e40d9320eab0af5d3048c11c1dc2dfd22f8c0cf#rd\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134082000,\"realSuperChapterId\":152,\"selfVisible\":0,\"shareDate\":1601129949000,\"shareUser\":\"鸿洋\",\"superChapterId\":153,\"superChapterName\":\"framework\",\"tags\":[],\"title\":\"面试官带你学安卓 - 从 View 的绘制流程说起\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":229,\"chapterName\":\"AOP\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15413,\"link\":\"https://mp.weixin.qq.com/s?__biz=MzUzOTk2MDUxMw==&amp;mid=2247484076&amp;idx=1&amp;sn=e06a95632487c5d3975ecdfce8ef5295&amp;chksm=fac13702cdb6be14325e125a269d1db20335291867d2380f1e0dbabf887b324899fdb87294ec#rd\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134071000,\"realSuperChapterId\":225,\"selfVisible\":0,\"shareDate\":1601129978000,\"shareUser\":\"鸿洋\",\"superChapterId\":227,\"superChapterName\":\"注解 & 反射 & AOP\",\"tags\":[],\"title\":\"手把手教大家用Transform API和ASM实现一个防快速点击案例\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":78,\"chapterName\":\"性能优化\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15414,\"link\":\"https://www.androidperformance.com/2018/05/07/Android-performance-optimization-skills-and-tools/\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134056000,\"realSuperChapterId\":53,\"selfVisible\":0,\"shareDate\":1601130226000,\"shareUser\":\"鸿洋\",\"superChapterId\":196,\"superChapterName\":\"热门专题\",\"tags\":[],\"title\":\"「置顶」Android 性能优化必知必会 &middot; Android Performance\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":229,\"chapterName\":\"AOP\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15415,\"link\":\"https://mp.weixin.qq.com/s?__biz=MzA5ODQ1ODU5NA==&amp;mid=2247484406&amp;idx=1&amp;sn=5e2929c1fe3f7aea41369c5d7ebf5933&amp;chksm=90900d7ba7e7846dcacc90dd557e709824fb642d1ded4068fe6f86aa2098673b9d0d9e6a90b8#rd\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134036000,\"realSuperChapterId\":225,\"selfVisible\":0,\"shareDate\":1601130290000,\"shareUser\":\"鸿洋\",\"superChapterId\":227,\"superChapterName\":\"注解 & 反射 & AOP\",\"tags\":[],\"title\":\"一起用Gradle Transform API + ASM完成代码织入呀～\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":313,\"chapterName\":\"字节码\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":true,\"id\":15416,\"link\":\"https://juejin.im/post/6847009773712310280\",\"niceDate\":\"17小时前\",\"niceShareDate\":\"18小时前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1601134017000,\"realSuperChapterId\":244,\"selfVisible\":0,\"shareDate\":1601130476000,\"shareUser\":\"鸿洋\",\"superChapterId\":245,\"superChapterName\":\"Java深入\",\"tags\":[],\"title\":\"JVM 角度看代码优化\",\"type\":0,\"userId\":2,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15384,\"link\":\"https://juejin.im/post/6844903843231170567\",\"niceDate\":\"2天前\",\"niceShareDate\":\"2天前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600996564000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1600996564000,\"shareUser\":\"躬行之\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"Material Design组件之CollapsingToolbarLayout\",\"type\":0,\"userId\":23270,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"鸿洋\",\"canEdit\":false,\"chapterId\":408,\"chapterName\":\"鸿洋\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15402,\"link\":\"https://mp.weixin.qq.com/s/qw18AbPKkvnQbvKqJ0Z2ig\",\"niceDate\":\"2天前\",\"niceShareDate\":\"1天前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600963200000,\"realSuperChapterId\":407,\"selfVisible\":0,\"shareDate\":1601043176000,\"shareUser\":\"\",\"superChapterId\":408,\"superChapterName\":\"公众号\",\"tags\":[{\"name\":\"公众号\",\"url\":\"/wxarticle/list/408/1\"}],\"title\":\"吹爆系列：OOM 不是你们想的那么简单！\",\"type\":0,\"userId\":-1,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"郭霖\",\"canEdit\":false,\"chapterId\":409,\"chapterName\":\"郭霖\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15403,\"link\":\"https://mp.weixin.qq.com/s/uzwE0RDI9RtY3mEV73ZTYQ\",\"niceDate\":\"2天前\",\"niceShareDate\":\"1天前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600963200000,\"realSuperChapterId\":407,\"selfVisible\":0,\"shareDate\":1601043190000,\"shareUser\":\"\",\"superChapterId\":408,\"superChapterName\":\"公众号\",\"tags\":[{\"name\":\"公众号\",\"url\":\"/wxarticle/list/409/1\"}],\"title\":\"Kotlin下的5种单例模式\",\"type\":0,\"userId\":-1,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15370,\"link\":\"https://www.jianshu.com/p/bd0659cf7ca6\",\"niceDate\":\"2天前\",\"niceShareDate\":\"2天前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600938876000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1600938876000,\"shareUser\":\"一介布衣\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"3D滚轮效果数据选择器\",\"type\":0,\"userId\":76843,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15359,\"link\":\"https://juejin.im/post/6875898896082534407/\",\"niceDate\":\"2020-09-24 12:02\",\"niceShareDate\":\"2020-09-24 12:02\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600920164000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1600920164000,\"shareUser\":\"renxhui\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"Android OpenGl Es 学习（三）：编译着色器\",\"type\":0,\"userId\":27931,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15350,\"link\":\"https://juejin.im/post/6844903842543304712\",\"niceDate\":\"2020-09-24 09:08\",\"niceShareDate\":\"2020-09-24 09:08\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600909735000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1600909735000,\"shareUser\":\"躬行之\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"Material Design组件之AppBarLayout\",\"type\":0,\"userId\":23270,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"\",\"canEdit\":false,\"chapterId\":502,\"chapterName\":\"自助\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15348,\"link\":\"https://www.jianshu.com/p/d4ce8e21027c\",\"niceDate\":\"2020-09-24 02:40\",\"niceShareDate\":\"2020-09-24 02:40\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600886449000,\"realSuperChapterId\":493,\"selfVisible\":0,\"shareDate\":1600886449000,\"shareUser\":\"彭旭锐\",\"superChapterId\":494,\"superChapterName\":\"广场Tab\",\"tags\":[],\"title\":\"数据结构 | 并查集（选学）\",\"type\":0,\"userId\":30587,\"visible\":1,\"zan\":0},{\"apkLink\":\"\",\"audit\":1,\"author\":\"鸿洋\",\"canEdit\":false,\"chapterId\":408,\"chapterName\":\"鸿洋\",\"collect\":false,\"courseId\":13,\"desc\":\"\",\"descMd\":\"\",\"envelopePic\":\"\",\"fresh\":false,\"id\":15372,\"link\":\"https://mp.weixin.qq.com/s/XZIf5tCU03Upscdl1mQ8qA\",\"niceDate\":\"2020-09-24 00:00\",\"niceShareDate\":\"2天前\",\"origin\":\"\",\"prefix\":\"\",\"projectLink\":\"\",\"publishTime\":1600876800000,\"realSuperChapterId\":407,\"selfVisible\":0,\"shareDate\":1600957346000,\"shareUser\":\"\",\"superChapterId\":408,\"superChapterName\":\"公众号\",\"tags\":[{\"name\":\"公众号\",\"url\":\"/wxarticle/list/408/1\"}],\"title\":\"教大家如何学习原理，放弃硬背 ，以 ThreadLocal 为例\",\"type\":0,\"userId\":-1,\"visible\":1,\"zan\":0}],\"offset\":0,\"over\":false,\"pageCount\":464,\"size\":20,\"total\":9272},\"errorCode\":0,\"errorMsg\":\"\"}";

                final ArticleEntity articleEntity = new Gson().fromJson(result, ArticleEntity.class);
                if (articleEntity.getErrorCode() == 0) {

                    final List<ArticleEntity.DataBean.DatasBean> datas = articleEntity.getData().getDatas();
                    HandlerUtil.post(new Runnable() {
                        @Override
                        public void run() {

                            if (mCurrentPage == 0) {
                                mBeanList.clear();
                            }

                            mBeanList.addAll(datas);
                            homeFragmentAdapter.notifyDataSetChanged();

                            mFresh.setRefreshing(false);
                        }
                    });
                }


            }
        });
    }

}
